package com.hd.trans.framework.dialog;

import android.app.Dialog;
import android.content.res.Resources;
import android.graphics.Color;
import android.graphics.drawable.ColorDrawable;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.Window;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.LinearLayout;
import android.widget.TextView;


import com.hd.trans.R;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.fragment.app.DialogFragment;

public class ConfirmDialog extends DialogFragment {
    private ConfirmDialogModule module;
    private TextView text;
    private TextView cancel;
    private TextView confirm;
    private TextView title;
    private CheckBox checkBox;
    private View llTips;

    private ConfirmDialog() {
    }

    protected ConfirmDialog(ConfirmDialogModule module) {
        this.module = module;
    }

    public static Builder builder() {
        return new Builder();
    }

    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View view = LayoutInflater.from(getContext()).inflate(R.layout.dialog_comfirm_layout, null);
        initDialog();
        initView(view);
        initData();
        return view;
    }

    private void initDialog() {
        Dialog dialog = getDialog();
        dialog.getWindow().setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT));//设置背景为透明
        dialog.setCancelable(false);
        dialog.setCanceledOnTouchOutside(false);
        dialog.requestWindowFeature(Window.FEATURE_NO_TITLE);
    }

    private void initView(View view) {
        text = view.findViewById(R.id.text);
        cancel = view.findViewById(R.id.cancel);
        confirm = view.findViewById(R.id.confirm);
        title = view.findViewById(R.id.title);
        checkBox = view.findViewById(R.id.check_box);
        llTips = view.findViewById(R.id.ll_tips);
    }

    private void initData() {
        if (!TextUtils.isEmpty(module.getTitle())) {
            title.setVisibility(View.VISIBLE);
            title.setText(module.getTitle());
        } else {
            title.setVisibility(View.GONE);
        }
        //内容
        if (!TextUtils.isEmpty(module.getContent()))
            text.setText(module.getContent());
        //左按钮文字
        if (!TextUtils.isEmpty(module.getLeftText()))
            cancel.setText(module.getLeftText());
        //右按钮文字
        if (!TextUtils.isEmpty(module.getRightText()))
            confirm.setText(module.getRightText());
        //左文字颜色
        if (module.isSetLeftColor())
            cancel.setTextColor(module.getLeftTextColor());
        //右文字颜色
        if (module.isSetRightColor())
            confirm.setTextColor(module.getRightTextColor());
        //checkBox
        if(module.isShowCheckBox()){
            llTips.setVisibility(View.VISIBLE);
            checkBox.setChecked(module.isChecked());
        }

        checkBox.setOnCheckedChangeListener((buttonView, isChecked) -> {
            if(null != module.getCheckedChangeListener())
                module.getCheckedChangeListener().onCheckedChanged(buttonView, isChecked);
        });


        //左按钮点击事件
        cancel.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (null != module.getLeftListenner())
                    module.getLeftListenner().OnComfrim(getDialog());
                getDialog().cancel();

            }
        });
        confirm.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (null != module.getRightListenner())
                    module.getRightListenner().OnComfrim(getDialog());
                dismiss();
            }
        });
    }

    @Override
    public void onStart() {
        super.onStart();
        Dialog dialog = getDialog();
        //设置dialog 宽高
        if (dialog != null) {
            int width = Resources.getSystem().getDisplayMetrics().widthPixels;
            dialog.getWindow().setLayout((int) (width * 0.75), LinearLayout.LayoutParams.WRAP_CONTENT);
        }
    }


    public static class Builder {

        private ConfirmDialogModule module;

        Builder() {
            module = new ConfirmDialogModule();
        }

        public ConfirmDialog build() {
            return new ConfirmDialog(module);
        }

        public Builder setContent(String content) {
            module.setContent(content);
            return this;
        }

        public Builder leftTxt(String leftText) {
            module.setLeftText(leftText);
            return this;
        }

        public Builder rightTxt(String rightText) {
            module.setRightText(rightText);
            return this;
        }

        public Builder leftTxtColor(int color) {
            module.setLeftTextColor(color);
            module.setSetLeftColor(true);
            return this;
        }

        public Builder leftTxtColor(String color) {
            if (isColor(color)) {
                module.setLeftTextColor(Color.parseColor(color));
                module.setSetLeftColor(true);
            } else {
                module.setSetLeftColor(false);
            }
            return this;
        }

        public Builder rightTxtColor(int color) {
            module.setRightTextColor(color);
            module.setSetRightColor(true);
            return this;
        }

        public Builder rightTxtColor(String color) {
            if (isColor(color)) {
                module.setRightTextColor(Color.parseColor(color));
                module.setSetRightColor(true);
            } else {
                module.setSetRightColor(false);
            }
            return this;
        }

        public Builder setTitle(String title) {
            if (!TextUtils.isEmpty(title))
                module.setTitle(title);
            return this;
        }

        public Builder contentColor(String color) {
            if (isColor(color)) {
                module.setContentColor(Color.parseColor(color));
                module.setSetContentColor(true);
            } else {
                module.setSetContentColor(false);
            }
            return this;
        }

        public Builder setLeftListenner(OnComfrimDialogListenner listenner) {
            module.setLeftListenner(listenner);
            return this;
        }

        public Builder setRightListenner(OnComfrimDialogListenner listenner) {
            module.setRightListenner(listenner);
            return this;
        }

        public Builder setShowCheckBox(boolean isShowCheckBox){
            module.setShowCheckBox(isShowCheckBox);
            return this;
        }

        public Builder setChecked(boolean isChecked){
            module.setChecked(isChecked);
            return this;
        }

        public Builder setOnCheckedChangeListener(CompoundButton.OnCheckedChangeListener listener){
            module.setCheckedChangeListener(listener);
            return this;
        }

        private boolean isColor(String color) {
            return !TextUtils.isEmpty(color) && color.matches("^#([a-fA-F\\d]{6}|[a-fA-F\\d]{3}|[a-fA-F\\d]{8})$");
        }

    }


    public interface OnComfrimDialogListenner {
        void OnComfrim(Dialog dialog);
    }
}
